﻿/*
 * File: ThreeDeeGamePlayBaseScene_LoadContent.cs
 * Author: Gourav Das
 * Purpose: Load Content for 3D gameplay
 * Created: June 19 2011
 */

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.IO;
using _22yards._2D;
using _22yards.Scenes.Helpers;
using _22yards.Core;
using _22yards;
using _22yards.EnumDeclarations;
using _22yards.ThreeD;
using _22yards.Helpers;
using Komires.MataliPhysics;

namespace _22yards.Scenes
{
    /// <summary>
    /// This is a scene that implements the 2D Gameplay screen
    /// </summary>
    /// 
    partial class GamePlayBaseScene : Scene
    {
        private const float STUMPHALFHEIGHT = 7.1f;/*Actual half height is 7.1f but we need to discount the bottom conical part that is 1.57; */
        private const float PHYSICSSTUMPHALFHEIGHT = 6.41f;
        
        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        private void Load3DSceneContent()
        {
            //RenderTarget for Base screen
            RenderTarget3DScene = new RenderTarget2D(sceneEngine.GraphicsDevice, sceneEngine.GraphicsDevice.Viewport.Width, sceneEngine.GraphicsDevice.Viewport.Height, true, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 8, RenderTargetUsage.PreserveContents);
            ShadowMapTarget = new RenderTarget2D(sceneEngine.GraphicsDevice, 2048, 2048, true, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 8, RenderTargetUsage.PreserveContents);

            RenderTargetBloom = new RenderTarget2D(sceneEngine.GraphicsDevice, sceneEngine.GraphicsDevice.Viewport.Width, sceneEngine.GraphicsDevice.Viewport.Height, true, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 8, RenderTargetUsage.PreserveContents);
            RenderTargetBlurBloom = new RenderTarget2D(sceneEngine.GraphicsDevice, sceneEngine.GraphicsDevice.Viewport.Width, sceneEngine.GraphicsDevice.Viewport.Height, true, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 8, RenderTargetUsage.PreserveContents);
            RenderTargetBlurIIBloom = new RenderTarget2D(sceneEngine.GraphicsDevice, sceneEngine.GraphicsDevice.Viewport.Width, sceneEngine.GraphicsDevice.Viewport.Height, true, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 8, RenderTargetUsage.PreserveContents);

            //Broadcast Camera
            broadcastCamera = new BroadcastCamera(sceneEngine.Game);
            sceneEngine.Game.Components.Add(broadcastCamera);

            broadcastBallChaseCam = new BroadcastBallChaseCamera(sceneEngine.Game);
            sceneEngine.Game.Components.Add(broadcastBallChaseCam);
            
            #region Load 3D Models

            KBRedBall = new BallModel(content.Load<Model>(@"ThreeDEE/Models/KBRedBall/ball"), 
                new Vector3(0f, 0f, 0f), 
                Vector3.Zero, 
                new Vector3(3.6f, 3.6f, 3.6f), 
                sceneEngine.GraphicsDevice, 
                "KBRedBall", 1);

            ShapePrimitive BallPrimitive = PhysxScene.Factory.ShapePrimitiveManager.Create("Ball Primitive");
            BallPrimitive.CreateSphere(PHYSICSSTUMPHALFHEIGHT / 9);

            Shape BallShape;
            BallShape = PhysxScene.Factory.ShapeManager.Create("Ball Shape");
            BallShape.Set(BallPrimitive, Matrix.Identity, 0f);

            BallShape.CreateMesh(0.1f);//Populates the Mesh information needed to render Physics object

            BallObject = PhysxScene.Factory.PhysicsObjectManager.Create("Ball");
            BallObject.Shape = BallShape;
            BallObject.InitLocalTransform.SetPosition(new Vector3(2f, 35f, -120f));
            BallObject.InitLocalTransform.SetLinearVelocity(new Vector3(-.3f, -5, 45f));
            BallObject.Integral.SetDensity(1.0f);
            BallObject.SetLocalGravityDirection(new Vector3(0, -0.5f, 0));
            BallObject.Material.Restitution = 0.15f;
            BallObject.Material.DynamicFriction = 1f;
            BallObject.InitLocalTransform.SetAngularVelocity(0, 0f, 0f);
            BallObject.InitLocalTransform.SetScale(1f, 1f, 1f);
            KBRedBall.PhysicsObject = BallObject;
            PhysxScene.UpdateFromInitLocalTransform(BallObject);


            Fielder1 = new FielderModel(content.Load<Model>(@"ThreeDEE/Players/fielder"),
                new Vector3(-15f * Game22Yards.scaleFactor.widthScale, 5f * Game22Yards.scaleFactor.htScale, 49f),
                new Vector3(0f, 0f, 0f),
                new Vector3(0.35f * Game22Yards.scaleFactor.widthScale,
                    0.35f * Game22Yards.scaleFactor.htScale, 0.35f),
                sceneEngine.GraphicsDevice,
                "Fielder1", 1);

            //StadiumModel
            stadia = new StadiumModel(content.Load<Model>("ThreeDEE/Models/Stadiums/Base/stadium"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, -0.20f * Game22Yards.scaleFactor.htScale, 49f),
                new Vector3(0f, 0f, 0f),
                new Vector3(26f * Game22Yards.scaleFactor.widthScale, 1f * Game22Yards.scaleFactor.htScale, 26f),
                sceneEngine.GraphicsDevice,
                "Stadia1", 1);

            //Physics Object for Field
            //TriangleMesh FieldTriangleMesh = PhysxScene.Factory.TriangleMeshManager.Create("Field Triangle Mesh");
            //FieldTriangleMesh.CreateFromModel(stadia.model, Matrix.Identity, false, false);

            //int FieldtriangleCount = FieldTriangleMesh.GetTriangleCount();
            //float[] Fieldfrictions = new float[FieldtriangleCount];
            //float[] Fieldrestitutions = new float[FieldtriangleCount];

            //for (int i = 0; i < FieldtriangleCount; i++)
            //{
            //    Fieldfrictions[i] = 1.0f;
            //    Fieldrestitutions[i] = 0.0f;
            //}
            ShapePrimitive FieldShapePrimitive = PhysxScene.Factory.ShapePrimitiveManager.Create("SP");
            FieldShapePrimitive.CreateCylinderY(.1f, 500f);
            //sp.CreateConvex(FieldTriangleMesh);
            //sp.CreateTriangleMesh(300, frictions, restitutions, 0f, 0f, FieldTriangleMesh);

            Shape FieldShape = PhysxScene.Factory.ShapeManager.Create("SSP");
            FieldShape.Set(FieldShapePrimitive, Matrix.Identity, 0.1f);
            FieldShape.CreateMesh(0.1f);//Populates the Mesh information needed to render Physics object

            FieldObject = PhysxScene.Factory.PhysicsObjectManager.Create("Field");
            FieldObject.Shape = FieldShape;
            FieldObject.UserTagStr = "FieldMesh";
            FieldObject.InitLocalTransform.SetPosition(new Vector3(0, -0.3f, 49));
            FieldObject.Material.DynamicFriction = 1f;
            FieldObject.Material.StaticFriction = 1f;
            FieldObject.Material.Restitution = 0.1f;
            FieldObject.EnableMoving = false;
            //stadia.PhysicsObject = FieldObject;
            PhysxScene.UpdateFromInitLocalTransform(FieldObject);

            //Physics Object for Rope
            //TriangleMesh RopetriangleMesh = null;
            //ShapePrimitive RopeshapePrimitive = null;
            //Shape Ropeshape = null;

            //RopetriangleMesh = PhysxScene.Factory.TriangleMeshManager.Create("TorusMesh2");
            //RopetriangleMesh.CreateTorusY(1000, 1500, 480, 0.25f);

            //int RopetriangleCount = RopetriangleMesh.GetTriangleCount();
            //float[] Ropefrictions = new float[RopetriangleCount];
            //float[] Roperestitutions = new float[RopetriangleCount];

            //for (int i = 0; i < RopetriangleCount; i++)
            //{
            //    Ropefrictions[i] = 1.0f;
            //    Roperestitutions[i] = 0.0f;
            //}

            //RopeshapePrimitive = PhysxScene.Factory.ShapePrimitiveManager.Create("TorusMesh");
            //RopeshapePrimitive.CreateTriangleMesh(300, Ropefrictions, Roperestitutions, 1.0f, 0.0f, RopetriangleMesh);

            //Ropeshape = PhysxScene.Factory.ShapeManager.Create("TorusMesh");
            //Ropeshape.Set(RopeshapePrimitive, Matrix.Identity, 0.1f);

            //RopeObject = PhysxScene.Factory.PhysicsObjectManager.Create("Rope");
            //RopeObject.Shape = Ropeshape;
            //RopeObject.InitLocalTransform.SetPosition(new Vector3(0, 0.25f, 49));
            //RopeObject.Material.DynamicFriction = 1f;
            //RopeObject.EnableMoving = false;
            //PhysxScene.UpdateFromInitLocalTransform(RopeObject);

            Pitch = new PitchModel(content.Load<Model>("ThreeDEE/Models/Stadiums/Pitches/pitch"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(1f * Game22Yards.scaleFactor.widthScale, 1f * Game22Yards.scaleFactor.htScale, 1f),
                sceneEngine.GraphicsDevice,
                "Pitch", 1);
            Pitch.HeightScale = 0.002f;
            //Physics Object for pitch
            //TriangleMesh RopetriangleMesh = null;

            ShapePrimitive PitchshapePrimitive = null;
            Shape Pitchshape = null;

            PitchshapePrimitive = PhysxScene.Factory.ShapePrimitiveManager.Create("Pitch Shape Primitive");
            PitchshapePrimitive.CreateBox(57.5f, .1f, 213);
            Pitchshape = PhysxScene.Factory.ShapeManager.Create("Pitch shape");
            Pitchshape.Set(PitchshapePrimitive, Matrix.Identity, 0f);
            Pitchshape.CreateMesh(0.1f);//Populates the Mesh information needed to render Physics object
            
            PitchObject = PhysxScene.Factory.PhysicsObjectManager.Create("Pitch");
            PitchObject.Shape = Pitchshape;
            PitchObject.InitLocalTransform.SetPosition(new Vector3(0, -0.1f, 49));
            PitchObject.Material.DynamicFriction = 1f;
            PitchObject.EnableMoving = false;
            PitchObject.Material.Restitution = 0.40f;
            //PitchObject.Integral.SetDensity(5000f);
            //PitchObject.InitLocalTransform.SetScale(1f);
            Pitch.PhysicsObject = PitchObject;
            //PitchObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(PitchObject);

            

            //Stump Shapes
            ShapePrimitive StumpPrimitive = PhysxScene.Factory.ShapePrimitiveManager.Create("Stump Primitive");
            StumpPrimitive.CreateCylinderY(PHYSICSSTUMPHALFHEIGHT * 2, STUMPHALFHEIGHT/32);
            //StumpPrimitive.CreateBox(STUMPHALFHEIGHT / 40, PHYSICSSTUMPHALFHEIGHT, STUMPHALFHEIGHT / 40);

            Shape StumpShape;
            StumpShape = PhysxScene.Factory.ShapeManager.Create("Stump Shape");
            StumpShape.Set(StumpPrimitive, Matrix.Identity, 0f);
            StumpShape.CreateMesh(0.1f); //Populates the Mesh information needed to render Physics object
            

            LeftStumpFarEndObject = PhysxScene.Factory.PhysicsObjectManager.Create("Left Stump FarEnd");
            LeftStumpFarEndObject.Shape = StumpShape;

            CenterStumpFarEndObject = PhysxScene.Factory.PhysicsObjectManager.Create("Center Stump FarEnd");
            CenterStumpFarEndObject.Shape = StumpShape;

            RightStumpFarEndObject = PhysxScene.Factory.PhysicsObjectManager.Create("Right Stump FarEnd");
            RightStumpFarEndObject.Shape = StumpShape;

            LeftStumpNearEndObject = PhysxScene.Factory.PhysicsObjectManager.Create("Left Stump NearEnd");
            LeftStumpNearEndObject.Shape = StumpShape;

            CenterStumpNearEndObject = PhysxScene.Factory.PhysicsObjectManager.Create("Center Stump NearEnd");
            CenterStumpNearEndObject.Shape = StumpShape;

            RightStumpNearEndObject = PhysxScene.Factory.PhysicsObjectManager.Create("Right Stump NearEnd");
            RightStumpNearEndObject.Shape = StumpShape;

           //Stump Models
            LeftStumpFarEnd = new StumpModel(content.Load<Model>("ThreeDEE/Models/Stumps/sidestump01"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(4.3f * Game22Yards.scaleFactor.widthScale, 4.7f * Game22Yards.scaleFactor.htScale, 4.3f),
                sceneEngine.GraphicsDevice,
                "LeftStump FarEnd", 1);

            LeftStumpFarEndObject.InitLocalTransform.SetPosition(new Vector3(1.98f, PHYSICSSTUMPHALFHEIGHT, 224f));
            LeftStumpFarEndObject.Material.DynamicFriction = 0.9f;
            LeftStumpFarEndObject.Material.StaticFriction = 0.9f;
            LeftStumpFarEndObject.Integral.SetDensity(2f);
            LeftStumpFarEndObject.Material.Restitution = 0f;
            //LeftStumpFarEndObject.MaxPreIntegrationAngularVelocity = 0f;
            //LeftStumpFarEndObject.MaxPostIntegrationAngularVelocity = 4f;
            //LeftStumpFarEndObject.MaxPostIntegrationLinearVelocity = 0f;
            //LeftStumpFarEndObject.MaxPreIntegrationLinearVelocity = 0f;
            LeftStumpFarEnd.PhysicsObject = LeftStumpFarEndObject;
            //LeftStumpFarEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(LeftStumpFarEndObject);
            

            RightStumpFarEnd = new StumpModel(content.Load<Model>("ThreeDEE/Models/Stumps/sidestump02"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(4.3f * Game22Yards.scaleFactor.widthScale, 4.7f * Game22Yards.scaleFactor.htScale, 4.3f),
                sceneEngine.GraphicsDevice,
                "RightStump FarEnd", 3);
            RightStumpFarEndObject.InitLocalTransform.SetPosition(new Vector3(-1.98f, PHYSICSSTUMPHALFHEIGHT, 224f));
            RightStumpFarEndObject.Material.DynamicFriction = 0.9f;
            RightStumpFarEndObject.Material.StaticFriction = 0.9f;
            RightStumpFarEndObject.Integral.SetDensity(2f);
            RightStumpFarEndObject.Material.Restitution = 0f;
            //RightStumpFarEndObject.MaxPostIntegrationAngularVelocity = 0f;
            //RightStumpFarEndObject.MaxPostIntegrationLinearVelocity = 4f;
            //RightStumpFarEndObject.MaxPreIntegrationLinearVelocity = 0f;
            RightStumpFarEnd.PhysicsObject = RightStumpFarEndObject;
            //RightStumpFarEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(RightStumpFarEndObject);
            

            CenterStumpFarEnd = new StumpModel(content.Load<Model>("ThreeDEE/Models/Stumps/centerstump01"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(4.3f* Game22Yards.scaleFactor.widthScale, 4.7f* Game22Yards.scaleFactor.htScale, 4.3f),
                sceneEngine.GraphicsDevice,
                "CenterStump FarEnd", 2);
            CenterStumpFarEndObject.InitLocalTransform.SetPosition(new Vector3(0f, PHYSICSSTUMPHALFHEIGHT, 224f));
            CenterStumpFarEndObject.Material.DynamicFriction = 0.9f;
            CenterStumpNearEndObject.Material.StaticFriction = 0.9f;
            CenterStumpFarEndObject.Integral.SetDensity(2f);
            CenterStumpFarEndObject.Material.Restitution = 0f;
            //CenterStumpFarEndObject.InitLocalTransform.SetRotation(Matrix.CreateFromYawPitchRoll(MathHelper.ToRadians(180), 0, 0));
            //CenterStumpFarEndObject.MaxPreIntegrationAngularVelocity = 0f;
            //CenterStumpFarEndObject.MaxPostIntegrationAngularVelocity = 4f;
            //CenterStumpFarEndObject.MaxPostIntegrationLinearVelocity = 80f;
            //CenterStumpFarEndObject.MaxPreIntegrationLinearVelocity = 0f;
            CenterStumpFarEnd.PhysicsObject = CenterStumpFarEndObject;
            //CenterStumpFarEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(CenterStumpFarEndObject);
            

            LeftStumpNearEnd = new StumpModel(content.Load<Model>("ThreeDEE/Models/Stumps/sidestump03"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(4.3f * Game22Yards.scaleFactor.widthScale, 4.7f * Game22Yards.scaleFactor.htScale, 4.3f),
                sceneEngine.GraphicsDevice,
                "LeftStump NearEnd", 4);
            LeftStumpNearEndObject.InitLocalTransform.SetPosition(new Vector3(1.98f, PHYSICSSTUMPHALFHEIGHT, -126f));
            LeftStumpNearEndObject.Material.DynamicFriction = 0.9f;
            LeftStumpNearEndObject.Material.StaticFriction = 0.9f;
            LeftStumpNearEndObject.Integral.SetDensity(2f);
            LeftStumpNearEndObject.Material.Restitution = 0f;
            //LeftStumpNearEndObject.MaxPreIntegrationAngularVelocity = 0f;
            //LeftStumpNearEndObject.MaxPostIntegrationAngularVelocity = 4f;
            LeftStumpNearEnd.PhysicsObject = LeftStumpNearEndObject;
            //LeftStumpNearEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(LeftStumpNearEndObject);

            RightStumpNearEnd = new StumpModel(content.Load<Model>("ThreeDEE/Models/Stumps/sidestump04"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(4.3f * Game22Yards.scaleFactor.widthScale, 4.7f * Game22Yards.scaleFactor.htScale, 4.3f),
                sceneEngine.GraphicsDevice,
                "RightStump NearEnd", 6);
            RightStumpNearEndObject.InitLocalTransform.SetPosition(new Vector3(-1.98f, PHYSICSSTUMPHALFHEIGHT, -126f));
            RightStumpNearEndObject.Material.DynamicFriction = 1f;
            RightStumpNearEndObject.Material.StaticFriction = 1f;
            RightStumpNearEndObject.Integral.SetDensity(2f);
            RightStumpNearEndObject.Material.Restitution = 0f;
            //RightStumpNearEndObject.MaxPreIntegrationAngularVelocity = 0f;
            //RightStumpNearEndObject.MaxPostIntegrationAngularVelocity = 4f;
            RightStumpNearEnd.PhysicsObject = RightStumpNearEndObject;
            //RightStumpNearEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(RightStumpNearEndObject);
            

            CenterStumpNearEnd = new StumpModel(content.Load<Model>("ThreeDEE/Models/Stumps/centerstump02"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(4.3f * Game22Yards.scaleFactor.widthScale, 4.7f * Game22Yards.scaleFactor.htScale, 4.3f),
                sceneEngine.GraphicsDevice,
                "CenterStump NearEnd", 5);
            CenterStumpNearEndObject.InitLocalTransform.SetPosition(new Vector3(0f, PHYSICSSTUMPHALFHEIGHT, -126f));
            CenterStumpNearEndObject.Material.DynamicFriction = 1f;
            CenterStumpNearEndObject.Integral.SetDensity(2f);
            CenterStumpNearEndObject.Material.StaticFriction = 1f;
            CenterStumpNearEndObject.Material.Restitution = 0f;
            //CenterStumpNearEndObject.InitLocalTransform.SetRsotation(Matrix.CreateFromYawPitchRoll(MathHelper.ToRadians(180), 0, 0));
            //CenterStumpNearEndObject.MaxPreIntegrationAngularVelocity = 0f;
            //CenterStumpNearEndObject.MaxPostIntegrationAngularVelocity = 4f;
            CenterStumpNearEnd.PhysicsObject = CenterStumpNearEndObject;
            //CenterStumpNearEndObject.UpdateFromInitLocalTransform();
            PhysxScene.UpdateFromInitLocalTransform(CenterStumpNearEndObject);


            //Bail Shapes
            ShapePrimitive BailPrimitive = PhysxScene.Factory.ShapePrimitiveManager.Create("Bail Primitive");
            BailPrimitive.CreateCapsuleX(1.64f, 0.1f);
            //BailPrimitive.CreateBox(0.84f, 0.1f, 0.1f);


            Shape BailShape;
            BailShape = PhysxScene.Factory.ShapeManager.Create("Bail Shape");
            BailShape.Set(BailPrimitive, Matrix.Identity, 0.1f);
            BailShape.CreateMesh(0.1f);//Populates the Mesh information needed to render Physics object

            LeftBailFarEndObject = PhysxScene.Factory.PhysicsObjectManager.Create("Left Bail FarEnd");
            LeftBailFarEndObject.Shape = BailShape;

            RightBailFarEndObject = PhysxScene.Factory.PhysicsObjectManager.Create("Right Bail FarEnd");
            RightBailFarEndObject.Shape = BailShape;

            LeftBailNearEndObject = PhysxScene.Factory.PhysicsObjectManager.Create("Left Bail NearEnd");
            LeftBailNearEndObject.Shape = BailShape;

            RightBailNearEndObject = PhysxScene.Factory.PhysicsObjectManager.Create("Right Bail NearEnd");
            RightBailNearEndObject.Shape = BailShape;

            //Bails
            LeftBailFarEnd = new BailModel(content.Load<Model>("ThreeDEE/Models/Bails/bail01"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(10f * Game22Yards.scaleFactor.widthScale, 10f * Game22Yards.scaleFactor.htScale, 10f),
                sceneEngine.GraphicsDevice,
                "LeftBail Farend", 1);

            LeftBailFarEndObject.InitLocalTransform.SetPosition(new Vector3(1f, PHYSICSSTUMPHALFHEIGHT*2f + 0.26f, 224f));
            LeftBailFarEndObject.Material.DynamicFriction = 1f;
            LeftBailFarEndObject.EnableMoving = true;
            LeftBailFarEndObject.Integral.SetDensity(2f);
            LeftBailFarEndObject.Material.Restitution = 0f;
            LeftBailFarEndObject.InitLocalTransform.SetRotation(Matrix.CreateFromYawPitchRoll(0, 0, MathHelper.ToRadians(180)));
            LeftBailFarEnd.PhysicsObject = LeftBailFarEndObject;
            PhysxScene.UpdateFromInitLocalTransform(LeftBailFarEndObject);

            RightBailFarEnd = new BailModel(content.Load<Model>("ThreeDEE/Models/Bails/bail02"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(10f * Game22Yards.scaleFactor.widthScale, 10f * Game22Yards.scaleFactor.htScale, 10f),
                sceneEngine.GraphicsDevice,
                "RightBail Farend", 2);
            RightBailFarEndObject.InitLocalTransform.SetPosition(new Vector3(-1f, PHYSICSSTUMPHALFHEIGHT*2f + 0.26f, 224f));
            RightBailFarEndObject.Material.DynamicFriction = 1f;
            RightBailFarEndObject.EnableMoving = true;
            RightBailFarEndObject.Integral.SetDensity(2f);
            RightBailFarEndObject.Material.Restitution = 0f;
            RightBailFarEnd.PhysicsObject = RightBailFarEndObject;
            PhysxScene.UpdateFromInitLocalTransform(RightBailFarEndObject);


            LeftBailNearEnd = new BailModel(content.Load<Model>("ThreeDEE/Models/Bails/bail03"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(10f * Game22Yards.scaleFactor.widthScale, 10f * Game22Yards.scaleFactor.htScale, 10f),
                sceneEngine.GraphicsDevice,
                "LeftBail Nearend", 3);
            LeftBailNearEndObject.InitLocalTransform.SetPosition(new Vector3(1f, PHYSICSSTUMPHALFHEIGHT*2f +0.26f, -126f));
            LeftBailNearEndObject.Material.DynamicFriction = 1f;
            LeftBailNearEndObject.Material.StaticFriction = 1f;
            LeftBailNearEndObject.EnableMoving = true;
            LeftBailNearEndObject.Integral.SetDensity(2f);
            LeftBailNearEndObject.Material.Restitution = 0f;
            LeftBailNearEndObject.InitLocalTransform.SetRotation(Matrix.CreateFromYawPitchRoll(0, 0, MathHelper.ToRadians(180)));
            LeftBailNearEnd.PhysicsObject = LeftBailNearEndObject;
            PhysxScene.UpdateFromInitLocalTransform(LeftBailNearEndObject);

            RightBailNearEnd = new BailModel(content.Load<Model>("ThreeDEE/Models/Bails/bail04"),
                new Vector3(0f * Game22Yards.scaleFactor.widthScale, 0f * Game22Yards.scaleFactor.htScale, 0f),
                new Vector3(0f, 0f, 0f),
                new Vector3(10f * Game22Yards.scaleFactor.widthScale, 10f * Game22Yards.scaleFactor.htScale, 10f),
                sceneEngine.GraphicsDevice,
                "RightBail Nearend", 4);
            RightBailNearEndObject.InitLocalTransform.SetPosition(new Vector3(-1f, PHYSICSSTUMPHALFHEIGHT*2f + 0.26f, -126f));
            RightBailNearEndObject.Material.DynamicFriction = 1f;
            RightBailNearEndObject.Material.StaticFriction = 1f;
            RightBailNearEndObject.EnableMoving = true;
            RightBailNearEndObject.Integral.SetDensity(2f);
            RightBailNearEndObject.Material.Restitution = 0f;
            RightBailNearEnd.PhysicsObject = RightBailNearEndObject;
            PhysxScene.UpdateFromInitLocalTransform(RightBailNearEndObject);

            Vector3 position1 = Vector3.One;
            Quaternion orientation1 = Quaternion.Identity;
            Quaternion orientation2 = Quaternion.Identity;
            constraint = null;
            constraint = PhysxScene.Factory.ConstraintManager.Create("LeftStumpFarEnd-Ground Constraint");
            constraint.PhysicsObject1 = LeftStumpFarEndObject;
            constraint.PhysicsObject2 = PitchObject;
            constraint.PhysicsObject1.MainWorldTransform.GetPosition(ref position1);
            constraint.PhysicsObject1.MainWorldTransform.GetOrientation(ref orientation1);
            constraint.PhysicsObject2.MainWorldTransform.GetOrientation(ref orientation2);
            constraint.SetAnchor1(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetAnchor2(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetInitWorldOrientation1(ref orientation1);
            constraint.SetInitWorldOrientation2(ref orientation2);
            constraint.EnableBreak = true;
            constraint.MinBreakVelocity = 150.0f;
            constraint.EnableLimitAngleX = true;
            constraint.EnableLimitAngleY = true;
            constraint.EnableLimitAngleZ = true;
            constraint.MinLimitDegAngleX = -25.0f;
            constraint.MaxLimitDegAngleX = +25.0f;
            constraint.MinLimitDegAngleZ = -25.0f;
            constraint.MaxLimitDegAngleZ = +25.0f;
            constraint.LimitAngleMode = LimitAngleMode.EulerXZY;
            //constraint.EnableControlAngleX = true;
            //constraint.EnableControlAngleY = true;
            //constraint.EnableControlAngleZ = true;
            //constraint.EnableControlAngleWithDeformation = true;
            constraint.MinAngleDeformationVelocity = 5.0f;
            constraint.AngularDamping = 0.2f;
            constraint.Update();

            ////Constraint constraint = null;
            constraint = PhysxScene.Factory.ConstraintManager.Create("RightStumpFarEnd-Ground Constraint");
            constraint.PhysicsObject1 = RightStumpFarEndObject;
            constraint.PhysicsObject2 = PitchObject;
            constraint.PhysicsObject1.MainWorldTransform.GetPosition(ref position1);
            constraint.PhysicsObject1.MainWorldTransform.GetOrientation(ref orientation1);
            constraint.PhysicsObject2.MainWorldTransform.GetOrientation(ref orientation2);
            constraint.SetAnchor1(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetAnchor2(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetInitWorldOrientation1(ref orientation1);
            constraint.SetInitWorldOrientation2(ref orientation2);
            constraint.EnableBreak = true;
            constraint.MinBreakVelocity = 150.0f;
            constraint.EnableLimitAngleX = true;
            constraint.EnableLimitAngleY = true;
            constraint.EnableLimitAngleZ = true;
            constraint.MinLimitDegAngleX = -25.0f;
            constraint.MaxLimitDegAngleX = +25.0f;
            constraint.MinLimitDegAngleZ = -25.0f;
            constraint.MaxLimitDegAngleZ = +25.0f;
            constraint.LimitAngleMode = LimitAngleMode.EulerXZY;
            //constraint1.EnableControlAngleX = true;
            //constraint1.EnableControlAngleY = true;
            //constraint1.EnableControlAngleZ = true;
            //constraint.EnableControlAngleWithDeformation = true;
            constraint.MinAngleDeformationVelocity = 5.0f;
            constraint.AngularDamping = 0.2f;
            constraint.Update();

            ////Constraint constraint = null;
            constraint = PhysxScene.Factory.ConstraintManager.Create("CenterStumpFarEnd-Ground Constraint");
            constraint.PhysicsObject1 = CenterStumpFarEndObject;
            constraint.PhysicsObject2 = PitchObject;
            constraint.PhysicsObject1.MainWorldTransform.GetPosition(ref position1);
            constraint.PhysicsObject1.MainWorldTransform.GetOrientation(ref orientation1);
            constraint.PhysicsObject2.MainWorldTransform.GetOrientation(ref orientation2);
            constraint.SetAnchor1(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetAnchor2(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetInitWorldOrientation1(ref orientation1);
            constraint.SetInitWorldOrientation2(ref orientation2);
            constraint.EnableBreak = true;
            constraint.MinBreakVelocity = 150.0f;
            constraint.EnableLimitAngleX = true;
            constraint.EnableLimitAngleY = true;
            constraint.EnableLimitAngleZ = true;
            constraint.MinLimitDegAngleX = -25.0f;
            constraint.MaxLimitDegAngleX = +25.0f;
            constraint.MinLimitDegAngleZ = -25.0f;
            constraint.MaxLimitDegAngleZ = +25.0f;
            constraint.LimitAngleMode = LimitAngleMode.Quaternion;
            //constraint3.EnableControlAngleX = true;
            //constraint3.EnableControlAngleY = true;
            //constraint3.EnableControlAngleZ = true;
            //constraint.EnableControlAngleWithDeformation = true;
            constraint.MinAngleDeformationVelocity = 5.0f;
            constraint.AngularDamping = 0.2f;
            constraint.Update();

            //Constraint constraint3 = null;
            constraint = PhysxScene.Factory.ConstraintManager.Create("LeftStumpNearEnd-Ground Constraint");
            constraint.PhysicsObject1 = LeftStumpNearEndObject;
            constraint.PhysicsObject2 = PitchObject;
            constraint.PhysicsObject1.MainWorldTransform.GetPosition(ref position1);
            constraint.PhysicsObject1.MainWorldTransform.GetOrientation(ref orientation1);
            constraint.PhysicsObject2.MainWorldTransform.GetOrientation(ref orientation2);
            constraint.SetAnchor1(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetAnchor2(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetInitWorldOrientation1(ref orientation1);
            constraint.SetInitWorldOrientation2(ref orientation2);
            constraint.EnableBreak = true;
            constraint.MinBreakVelocity = 100.0f;
            constraint.EnableLimitAngleX = true;
            constraint.EnableLimitAngleY = true;
            constraint.EnableLimitAngleZ = true;
            constraint.MinLimitDegAngleX = -25.0f;
            constraint.MaxLimitDegAngleX = +25.0f;
            constraint.MinLimitDegAngleZ = -25.0f;
            constraint.MaxLimitDegAngleZ = +25.0f;
            constraint.LimitAngleMode = LimitAngleMode.EulerXZY;
            //constraint3.EnableControlAngleX = true;
            //constraint3.EnableControlAngleY = true;
            //constraint3.EnableControlAngleZ = true;
            //constraint.EnableControlAngleWithDeformation = true;
            constraint.MinAngleDeformationVelocity = 5.0f;
            constraint.AngularDamping = 0.2f;
            constraint.Update();

            //Constraint constraint3 = null;
            constraint = PhysxScene.Factory.ConstraintManager.Create("RightStumpNearEnd-Ground Constraint");
            constraint.PhysicsObject1 = RightStumpNearEndObject;
            constraint.PhysicsObject2 = PitchObject;
            constraint.PhysicsObject1.MainWorldTransform.GetPosition(ref position1);
            constraint.PhysicsObject1.MainWorldTransform.GetOrientation(ref orientation1);
            constraint.PhysicsObject2.MainWorldTransform.GetOrientation(ref orientation2);
            constraint.SetAnchor1(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetAnchor2(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetInitWorldOrientation1(ref orientation1);
            constraint.SetInitWorldOrientation2(ref orientation2);
            constraint.EnableBreak = true;
            constraint.MinBreakVelocity = 100.0f;
            constraint.EnableLimitAngleX = true;
            constraint.EnableLimitAngleY = true;
            constraint.EnableLimitAngleZ = true;
            constraint.MinLimitDegAngleX = -25.0f;
            constraint.MaxLimitDegAngleX = +25.0f;
            constraint.MinLimitDegAngleZ = -25.0f;
            constraint.MaxLimitDegAngleZ = +25.0f;
            constraint.LimitAngleMode = LimitAngleMode.EulerXZY;
            //constraint3.EnableControlAngleX = true;
            //constraint3.EnableControlAngleY = true;
            //constraint3.EnableControlAngleZ = true;
            constraint.EnableControlAngleWithDeformation = true;
            constraint.MinAngleDeformationVelocity = 5.0f;
            constraint.AngularDamping = 0.2f;
            constraint.Update();

            //Constraint constraint3 = null;
            constraint = PhysxScene.Factory.ConstraintManager.Create("CenterStumpNearEnd-Ground Constraint");
            constraint.PhysicsObject1 = CenterStumpNearEndObject;
            constraint.PhysicsObject2 = PitchObject;
            constraint.PhysicsObject1.MainWorldTransform.GetPosition(ref position1);
            constraint.PhysicsObject1.MainWorldTransform.GetOrientation(ref orientation1);
            constraint.PhysicsObject2.MainWorldTransform.GetOrientation(ref orientation2);
            constraint.SetAnchor1(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetAnchor2(position1 - new Vector3(0.0f, PHYSICSSTUMPHALFHEIGHT, 0.0f));
            constraint.SetInitWorldOrientation1(ref orientation1);
            constraint.SetInitWorldOrientation2(ref orientation2);
            constraint.EnableBreak = true;
            constraint.MinBreakVelocity = 100.0f;
            constraint.EnableLimitAngleX = true;
            constraint.EnableLimitAngleY = true;
            constraint.EnableLimitAngleZ = true;
            constraint.MinLimitDegAngleX = -25.0f;
            constraint.MaxLimitDegAngleX = +25.0f;
            constraint.MinLimitDegAngleZ = -25.0f;
            constraint.MaxLimitDegAngleZ = +25.0f;
            constraint.LimitAngleMode = LimitAngleMode.EulerXZY;
            //constraint3.EnableControlAngleX = true;
            //constraint3.EnableControlAngleY = true;
            //constraint3.EnableControlAngleZ = true;
            //constraint.EnableControlAngleWithDeformation = true;
            constraint.MinAngleDeformationVelocity = 5.0f;
            constraint.AngularDamping = 0.2f;
            constraint.Update();

            #endregion

            #region Load Textures for 3D Models

            //Ground Textures
            TexGround = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Base/Customground.png");
            TexGroundNormal = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Base/Customground_NRM.png");
            TexGroundSpec = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Base/Customground_SPE.png");
            TexGroundPitch = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Base/Custompitch.png");
            TexGroundPitchNormal = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Base/Custompitch_NRM.png");
            TexGroundPitchSpec = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Base/Custompitch_SPE.png");
            TexRope = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Base/Customrope.png");
            TexRopeNormal = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Base/Customrope_NRM.png");
            TexRopeSpec = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Base/Customrope_SPE.png");

            //Pitch Textures
            TexPitch = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Pitches/custompitch.png");
            TexPitchNormal = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Pitches/custompitch_NRM.png");
            TexPitchSpec = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Pitches/custompitch_SPEC.png");
            TexPitchHeight = content.Load<Texture2D>("ThreeDEE/Models/Stadiums/Pitches/pitch_height");
                //LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stadiums/Pitches/pitch_relief1.bmp");

            //StumpTextures
            TexSideStump = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stumps/sidestump01.png");
            TexCenterStump = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stumps/centerstump01.png");
            TexSideStumpNormal = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stumps/sidestump01_NRM.png");
            TexCenterStumpNormal = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stumps/centerstump01_NRM.png");
            TexSideStumpSpec = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stumps/sidestump01_SPEC.png");
            TexCenterStumpSpec = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Stumps/centerstump01_SPEC.png");

            //Bail Textures
            TexBail = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Bails/custombail.png");
            TexBailNormal = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Bails/custombail_NRM.png");
            TexBailSpec = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/Bails/custombail_SPEC.png");

            //Ball Textures
            TexKBRedBall = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/KBRedBall/customball.png");
            TexKBRedBallNormal = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/KBRedBall/customball_NRM.png");
            TexKBRedBallSpec = LoadTextureFromFile(@"22yardsContent/ThreeDEE/Models/KBRedBall/customball_SPEC.png");

            #endregion
        }
    }
}
